<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <style>table{border: 1px solid black;}</style>
<body>
    <div>        
    <table>
        <tr><td style="font-size: 18px;">添加博客或者修改</td></tr>
        <tr>
            <td><label for="">标题：</label></td>
            <td><input type="text" placeholder="请输入商品名称" name="BName"></td>
        </tr>
        <tr>
            <td><label for="">摘要：</label></td>
            <td><input type="text" placeholder="请输入摘要" name="Contend"></td>
        </tr>
        <tr>
            <td><label for="">cid：</label></td>
            <td><input type="text" placeholder="请输入摘要" name="CatagoryId"></td>
            <tr>
                <td><input type="button" value="保存" onclick="save()"></td>
                <td><input type="button" value="取消并返回" onclick="cancel()"></td>
            </tr>
        </tr>
    </table>
    <script src="./jquery-3.6.0.min.js"></script>
    <script src="./js/config/baseUrl.js"></script>
    <script src="./js/init.js"></script>
    <script>
        $(function () {
            let id = getUrlParam('id');
            id = id || 0;
            getbyid(id).then(res => {
                let obj = res.data;
               $('[name=BName]').val(obj.BName); 
               $('[name=Contend]').val(obj.Contend); 
               $('[name=CatagoryId]').val(obj.CatagoryId); 
            })
        })
         function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); 
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
        function save() {
            let id = getUrlParam('id');
            let obj = {
                BName: $('[name=BName]').val(),
                Contend: $('[name=Contend]').val(),
                CatagoryId: $('[name=CatagoryId]').val(),
            }
            if(id){
                updateblog(id,obj).then(res=>{
                    location.href='./index.html';
                })
            }else{
                  let params = {
                    BName: obj. BName,
                    Contend: obj.Contend,
                    CatagoryId: obj.CatagoryId,
                }
                addblog(params).then(res => {
                    location.href = './index.html';
                })}
              
           
        }
        function cancel(){
            location.href='./index.html'
        }
    </script>
</div>
</body>
</html>